/*
 *  sokoban - a Sokoban game for midp-supporting mobile devices
 *  Copyright (C) 2007,2009 Dedi Hirschfeld
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

package com.xomzom.midlets.sokoban;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;

/**
 * A text file view.
 * 
 * @author dedi
 */
public class TextView
{
	//
    // Members.
    //

    /**
     * The form object for this dialog. will be null if the dialog was not yet
     * initialized.
     */
    private Form m_form;

    /**
     * The previous displayable object - to display when this dialog is hidden.
     */
    private Displayable m_prevView;

    /**
     * The display this form is associated with.
     */
    private Display m_display;
    
    /**
     * The text file name.
     */
    private String m_textFileName;
    
    /**
     * The view title.
     */
    private String m_title;


    //
	// Operations.
	//

	/**
	 * Create a new splash screen, associated with the given display and file.
	 *  
	 * @param title The view title.
	 * @param textFileName The text file to display.
	 * @param display The display
	 */
	public TextView(String title, String textFileName, Display display)
	{
		m_title = title;
        m_display = display;
        m_textFileName = textFileName;
	}

    /**
     * Show the dialog.
     */
    public void show()
    {
        if (m_form == null)
        {
            m_form = new Form(m_title);
            init(m_form);
        }

        m_prevView = m_display.getCurrent();
        m_display.setCurrent(m_form);
    }

	/**
	 * Create the actual splash message.
	 */
	public void init(Form form)
	{
		String formText;
		try {
			formText = read(m_textFileName); 
		} catch (IOException e) {
			formText = "An error occured while reading the about file";
		}
		form.append(formText);
        final Command cmdOk = new Command("OK", Command.OK, 1);
        form.addCommand(cmdOk);

        form.setCommandListener(new CommandListener() {
            public void commandAction(Command c, Displayable d)
            {
                if (c == cmdOk) 
                {
                    m_display.setCurrent(m_prevView);
                }
            }
        });
	}

    /**
     * Read A text file, and return it's contents as a single string.
     * 
     * @param filename The filename to read from.
     * @return The file contents, as a string.
     * @throws IOException if something went wrong in reading. 
     */
    private String read(String filename) throws IOException
    {
        //InputStream inStream = new FileInputStream(filename);
        InputStream inStream = getClass().getResourceAsStream(filename);
        byte[] fileContents = new byte[inStream.available()];
        inStream.read(fileContents);
        inStream.close();
        return new String(fileContents);
    }
}
